

use "$home\persondata_v2", clear

drop if cvrnr==. 

keep pnr cvrnr year lonind
gen logwage=log(lonind)
drop if logwage==.

bysort cvrnr year: egen p10 = pctile(logwage), p(10)
bysort cvrnr year: egen p25 = pctile(logwage), p(25)
bysort cvrnr year: egen p50 = pctile(logwage), p(50)
bysort cvrnr year: egen p75 = pctile(logwage), p(75)
bysort cvrnr year: egen p90 = pctile(logwage), p(90)

gen nworker=1

drop pnr
collapse (mean) p* logwage (sum) nworker, by (cvrnr year)

preserve

compress
save "$project/wagedistribution"


restore

merge m:1 cvrnr year using "$project\manager\manager_all_cvrnr2", keep(1 3) keepus(pnr) nogen
ren pnr pnrm
merge m:1 pnrm using "$project/managerfe_1step_over5", keep(1 3) nogen
merge 1:1 cvrnr year using "$project\temp_ind_region", keep(1 3) nogen
merge 1:1 cvrnr year using "$project\manager\cvrnryear_fe_new", keep(1 3) nogen
ren fe cvrnryearfe
merge 1:1 cvrnr year using "$project\cvrnrfe_1step_over5", keep(1 3) nogen
ren cvrnrfe pe

gen gap_9010 = p90-p10

eststo clear

foreach var in p10 p25 p50 p75 p90 gap_9010 {
	eststo: reghdfe `var' pe [aw=nworker], a(year#ind127 year#region) 
}

estout using "$project\results\2024\withinfirm_inequality.xls", cells(b(fmt(4)) se(par fmt(4)))  legend stats(N, labels("No. of obs.")) varwidth(46) style(esttab) replace

